<?php

/**
 * advert actions.
 *
 * @package    explications_informatiques
 * @subpackage advert
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class advertActions extends sfActions
{


  public function executeNew(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->hasCredential("MODERATION_USER"),"headers","forbidden");
    $this->form = new AdvertsForm();
 	$this->form->setDefault('id_membre', $request->getParameter('id'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forwardUnless($request->isMethod(sfRequest::POST),"headers","forbidden");
	$this->forwardUnless($this->getUser()->hasCredential("MODERATION_USER"),"headers","forbidden");
    $this->form = new AdvertsForm();
    $this->processForm($request, $this->form);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->hasCredential("MODERATION_USER"),"headers","forbidden");
    $this->forward404Unless($adverts = Doctrine_Core::getTable('Adverts')->find(array($request->getParameter('idadverts'))), sprintf('Object adverts does not exist (%s).', $request->getParameter('idadverts')));
    $this->form = new AdvertsForm($adverts);
  }

  public function executeUpdate(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->hasCredential("MODERATION_USER"),"headers","forbidden");
    $this->forwardUnless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT),"headers","forbidden");
    $this->forward404Unless($adverts = Doctrine_Core::getTable('Adverts')->find(array($request->getParameter('idadverts'))), sprintf('Object adverts does not exist (%s).', $request->getParameter('idadverts')));
    $this->form = new AdvertsForm($adverts);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->hasCredential("MODERATION_USER"),"headers","forbidden");
    $this->forward404Unless($adverts = Doctrine_Core::getTable('Adverts')->find(array($request->getParameter('idadverts'))), sprintf('Object adverts does not exist (%s).', $request->getParameter('idadverts')));
    $adverts->delete();
	sfCoreAutoload::logElem("Suppression d'un avertissement - id:".$request->getParameter('idadverts'), $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
    $this->redirect('compte/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
	$this->forwardUnless($this->getUser()->hasCredential("MODERATION_USER"),"headers","forbidden");
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
	  if(!empty($request["adverts"]["id"]))
	  	$adverts = Doctrine_Core::getTable('adverts')->findOneByUserId($request["adverts"]["id"]);
	  else
	  	$adverts = new Adverts();
      $adverts->setIdMembre($request["adverts"]["id_membre"]);
	  $adverts->setIdAuteur($this->getUser()->getGuardUser()->getId());
	  $adverts->setRaison($request["adverts"]["raison"]);
	  $adverts->setDateAjout(date("Ymdhis"));
	  if($request["adverts"]["unlimited"] != "1")
	  	$adverts->setDateFin(date("Ymdhis",strtotime($advert["date_fin"],time())));
	  $adverts->setNewGroup($request["adverts"]["new_group"]);
	  $adverts->save();
	  $adverts->getSfGuardUserProfile()->decrPoint($adverts->getSfGuardUserProfile()->getNbPoint(),"Avertissement => 0 points!");
	  $mp = new Mp();
	  $mp->setIdAuteur($this->getUser()->getGuardUser()->getId());
	  $mp->setIdDestinataire($request["adverts"]["id_membre"]);
	  $mp->setSujet("Attention vous avez été averti!");
	  $mp->setMessage("Vous avez été averti pour un comportement non approprié. Veulliez consulter votre profil pour plus de détails");
	  $mp->setDate(date("Ymdhis"));
	  $mp->save();
	  //log de l'action en cours
	  sfCoreAutoload::logElem("Ajout d'un avertissement - cible:".$request["adverts"]["id_membre"], $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
	  $this->getUser()->setFlash("info","Membre averti");
      $this->redirect('compte/edit?id='.$request["adverts"]["id_membre"]);
    }
  }
}
